﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MagicArena.framework.state;
using MagicArena.framework.graphics;
using MagicArena.framework.toon;
using Microsoft.Xna.Framework;

namespace MagicArena.framework.action
{
    public abstract class AAction : IToonComponent, IUpdateable
    {
        #region Properties
        protected List<State> _StateChanges = new List<State>(1);
        public State[] StateChanges
        {
            get
            {
                return _StateChanges.ToArray();
            }
        }
        public string Name
        {
            get;
            private set;
        }
        protected ARenderer Rend
        {
            get;
            private set;
        }
        // TODO This is bad because it leads to circular dependancy
        protected Toon SourceToon
        {
            get;
            private set;
        }
        #endregion

        #region Constructors
        
        public AAction( string name)
        {
            Name = name;
        }
        #endregion

        #region Abstract Methods
        public virtual void Execute(Object target)
        {
            if (target is Toon)
            {
                Toon toon = (Toon)target;
                foreach (State s in _StateChanges)
                {
                    State ts = State.FindState(toon.States, s.Id);
                    ts.Value += s.Value;
                }
            }
            else
            {
                throw new ArgumentException("Cannot execute on non-toon target");
            }
        }
        #endregion

        #region Override methods
        public override bool Equals(object obj)
        {
            if (obj != null && obj is AAction)
            {

                if (Name.Equals(((AAction)obj).Name))
                    return true;

            }
            return false;
        }
        public override string ToString()
        {
            return string.Format("AAction[{0}]", Name);
        }
        #endregion

        #region IToonComponent
        public void AttachToToon(Toon t)
        {
            SourceToon = t;
        }
        #endregion


        #region IUpdatable methods
        #region Enable
        private bool _Enabled = false;
        public bool Enabled
        {
            get { return _Enabled; }
            set
            {
                if (_Enabled != value)
                {
                    _Enabled = value;
                    OnEnabledChanged(new EnabledEventArgs(value));
                }
                _Enabled = value;
            }
        }

        public event EventHandler<EventArgs> EnabledChanged;

        public class EnabledEventArgs : EventArgs
        {
            public bool NewState
            {
                get;
                private set;
            }
            public EnabledEventArgs(bool state)
            {
                NewState = state;
            }
        }
        protected virtual void OnEnabledChanged(EventArgs e)
        {
            if (EnabledChanged != null) EnabledChanged(this, e);
        }
        #endregion

        #region Update
        public abstract void Update(GameTime gameTime);

        public int UpdateOrder
        {
            get { return Int16.MaxValue / 2; }
        }

        public event EventHandler<EventArgs> UpdateOrderChanged;
        #endregion
        #endregion
    }
}
